﻿using Abp.Application.Services.Dto;
using Abp.Dependency;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Configuration;
using System;
using System.Linq;

namespace HCD.Web.Authorizer
{
    /// <summary>
    /// 
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public class PermissionAttribute : ActionFilterAttribute, ITransientDependency
    {
        /// <summary>
        ///
        /// </summary>
        public PermissionAttribute()
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public override void OnResultExecuted(ResultExecutedContext context)
        {
            base.OnResultExecuted(context);

            //var cache = (ICacheManager)context.HttpContext.RequestServices.GetService(typeof(ICacheManager));

            //var OperatorAppService = (IOperatorAppService)context.HttpContext.RequestServices.GetService(typeof(IOperatorAppService));

            //var auth = context.RouteData.Values.Where(a => a.Key == "auth").FirstOrDefault().Value as AuthModel;

            //var uid = auth.UserID;

            //var route = context.HttpContext.Request.Path.Value;

            //var flist = cache.GetCache(RedisConst.CacheFeature)
            //        .Get(uid.ToString(), () => { return OperatorAppService.GetPersonalCombinedFeature(new EntityDto<Guid>() { Id = uid }); });

            //var f = flist.FirstOrDefault(a => (a.Url ?? string.Empty).ToUpper().Split(',').Any(b => b == route.ToUpper()));
            //if (f != null)
            //{
            //    var service = (ILog4ActionAppService)context.HttpContext.RequestServices.GetService(typeof(ILog4ActionAppService));
            //    var log = new Log4ActionEditDto
            //    {
            //        ActionDateTime = DateTime.Now,
            //        ActionName = f.Title,
            //        ActionUrl = route,
            //        OperatorID = auth.UserID,
            //        RealName = auth.RealName
            //    };
            //    service.AddActionLog(log);
            //}
        }
    }
}